set more off

if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}

**** Table 1 – Balancing between "training, no loan" vs. "training and loan" *******************************
use "${gsdAnalysisOutput}/bscie_panel_regressions", clear

keep if wave==1 & final_sample == 1
drop if replacement == 1 & gender == 1

keep if treatment_stream==3 | treatment_stream==4
replace treatment_stream=0 if treatment_stream==3
replace treatment_stream=1 if treatment_stream==4
label define treatment_stream 0 "Training, no grant" 1 "Training and grant", modify
label values treatment_stream treatment_stream 

clear matrix
mat A = J(39,6,.)
mat stars = J(39,6,0)

local n 1
local replace replace
local append 

quietly tabulate edu_level, gen(edu_level_)
quietly tabulate lit_eval, gen(lit_eval_)
quietly tabulate num_eval, gen(num_eval_)
quietly tabulate state_old, gen(cat_state_assigned)

foreach var in age gender married ls_employed biz_own  ///
food_spend_total nonfood_spend_total account_yn formal_debt informal_debt ///
edu_level_1  edu_level_2 edu_level_3 edu_level_4 lit_eval_1 lit_eval_2 lit_eval_3  ///
num_eval_1 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist death_x_prox_b300 death_x_prox_a300 {


ttest `var' , by(treatment_stream)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if treatment_stream == 0
local N=`r(N)'
mat A[30,1]=`N'
display `N'
sum `var' if treatment_stream == 1
local N=`r(N)'
mat A[30,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}

********************************************************************************

reg treatment_stream age gender married biz_own  ///
food_spend_total  account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_b300 death_x_prox_a300

test age gender married biz_own  ///
food_spend_total account_yn formal_debt informal_debt ///
  edu_level_2 edu_level_3 edu_level_4  lit_eval_2 lit_eval_3  ///
 num_eval_2 num_eval_3 num_eval_4 ///
hh_size hhsize_kids hhsize_senior hh_rooms hh_build lg_kcb_dist ///
death_x_prox_b300 death_x_prox_a300

mat A[31,5] = `r(F)'
mat A[31,6] = `r(p)'
matrix stars[31,5] = (abs(A[31,5]/A[31,6]) > invttail(r(df_t),0.10/2)) + (abs(A[31,5]/A[31,6]) > invttail(r(df_t),0.05/2)) + (abs(A[31,5]/A[31,6]) > invttail(r(df_t),0.01/2))

local n 32

foreach var in cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 {

ttest `var' , by(treatment_stream)
local mean0 = `r(mu_1)'
display `mean0'
mat A[`n',1] = `mean0'
local sd0 = `r(sd_1)'
display `sd0'
mat A[`n',2] = `sd0'


local mean1 = `r(mu_2)'
display `mean1'
mat A[`n',3] = `mean1'
local sd1 = `r(sd_2)'
display `sd1'
mat A[`n',4] = `sd1'


local diff `mean1' - `mean0'
display `diff'
mat A[`n',5] = `diff'
local se = `r(se)'
display `se'
mat A[`n',6] = `se'

matrix stars[`n',5] = (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.10/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.05/2)) + (abs(A[`n',5]/A[`n',6]) > invttail(r(df_t),0.01/2))

sum `var' if treatment_stream == 0
local N=`r(N)'
mat A[38,1]=`N'
display `N'
sum `var' if treatment_stream == 1
local N=`r(N)'
mat A[38,3]=`N'
display `N'

local n `n'+1
display `n'

local mean0
local mean1
local N
local diff
local sd0
local se
}


reg treatment_stream cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6  

test cat_state_assigned1 cat_state_assigned2 cat_state_assigned3 cat_state_assigned4 cat_state_assigned5  cat_state_assigned6 

mat A[39,5] = `r(F)'
mat A[39,6] = `r(p)'
matrix stars[39,5] = (abs(A[39,5]/A[39,6]) > invttail(r(df_t),0.10/2)) + (abs(A[39,5]/A[39,6]) > invttail(r(df_t),0.05/2)) + (abs(A[39,5]/A[39,6]) > invttail(r(df_t),0.01/2))

matrix rownames A = "Age" "Female" "Married" "Employment status" "Business ownership" "(Log) food consumption food" "(Log) nonfood consumption" ///
"Formal bank account" "Formal loans in past 5 years" "Informal loans in past 5 years" "Education level: No education"  "Education level: Some primary" "Education level: Some secondary" "Education level: Some university" ///
"Literacy: No English" "Literacy: Some English" "Literacy: Good English"   "Numeracy: Low"  "Numeracy: Counting 0-100" "Numeracy: Addition" "Numeracy: Multiplication" ///
 "Household size" "Number of children" "Number of elderly" "Number of rooms" "Number of buildings" "(Log) distance to KCB branch" ///
  "Conflict exposure (2011-2014)" "Conflict exposure (2015-2017)" "Observations" "Joint orthogonality" "Central Equatoria" "Eastern Equatoria" "Lakes" "Northern Bahr El Ghazal" "Western Bahr El Ghazal" "Western Equatoria" "Observations" "Joint orthogonality"
frmttable , statmat(A) replace sdec(3) annotate(stars) asymbol(*,**,***) title (Table 1 – Balancing between "training, no loan" vs. "training and loan") 
frmttable using "$gsdTables/Table1.tex", replay tex replace  

